package top.zackyoung.bbs.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.zackyoung.bbs.entity.Comment;
import top.zackyoung.bbs.entity.Forum;
import top.zackyoung.bbs.mapper.CommentMapper;
import top.zackyoung.bbs.mapper.ForumMapper;
import top.zackyoung.bbs.utils.TextProcessUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: ZackYoung
 * @time: 2020/6/15
 */
@Service
public class ForumService {
    @Autowired
    private ForumMapper forumMapper;
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 加入关注
     *
     * @param forum
     * @return
     */

    public Map<String, String> insertForum(Forum forum) {
        Map map = new HashMap<>();
        Map error = new HashMap<>();
        map.put("success", "false");
        if (forum.getTagId() == null || forum.getTagId() == 0) {
            error.put("tagId", "标签不能为空");
        }
        if (forum.getContent().equals("")) {
            error.put("content", "话题内容不能为空");
        }
        if (forum.getTitle().equals("")) {
            error.put("title", "标题不能为空");
        }
        if (forum.getTagId() != null && forum.getTagId() != 0 && !forum.getContent().equals("") && !forum.getTitle().equals("")) {
            map.put("success", "true");
            forumMapper.insertForum(forum);
        } else {
            map.put("error", error);
        }

        return map;
    }

    /**
     * 查询关注数
     *
     * @param tagId
     * @return
     */

    public int queryForumCount(Integer tagId) {
        int count = forumMapper.queryForumCount(tagId);
        return count;
    }

    /**
     * 查询论坛文章
     *
     * @param forum
     * @param page
     * @param count
     * @return
     */

    public List<Forum> queryForum(Forum forum, Integer page, Integer count) {
        TextProcessUtil textProcessUtil = new TextProcessUtil();

        List<Forum> list = forumMapper.queryForum(forum.getTagId(), forum.getId(), page, count);
        for (Forum f : list) {
            // 格式化内容
            String content = f.getContent();
            List<String> imgUrl = textProcessUtil.getImgStr(content);
            f.setImgUrl(imgUrl);
            String reWriteContent = textProcessUtil.parseHtml(content, 300);
            f.setReWriteContent(reWriteContent);
        }
        return list;
    }

    /**
     * 查询评论
     *
     * @param commentObjectId
     * @param page
     * @param count
     * @param totalRecord
     * @return
     */
    public List<Comment> queryComment(Integer commentObjectId, Integer page, Integer count, int totalRecord) {
        // 查询文章评论
        List<Comment> queryForum = commentMapper.queryComment(1, commentObjectId, page, count);
        if (totalRecord > 0) {
            for (Comment c : queryForum) {
                // 查询评论回复
                List<Comment> queryReply = commentMapper.queryComment(3, c.getId(), null, null);
                c.setReplyList(queryReply);
            }
        }
        return queryForum;
    }

    /**
     * 查询评论的总数
     *
     * @param commentObjectId
     * @return
     */
    public int queryCommentCount(Integer commentObjectId) {
        return commentMapper.queryCommentCount(1, commentObjectId);
    }

    public int addComment(Integer userId, Integer type, Integer commentObjectId, String content) {
        int result = commentMapper.insertComment(userId, type, commentObjectId, content);
        return result;
    }

    public int viewNumIncrement(Integer tagId) {
        return forumMapper.viewNumIncrement(tagId);
    }
}
